package com.zprmb.udzt;

import java.text.DecimalFormat;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;

public class ExperienceGoldOutActivity extends BaseActivity implements OnClickListener{
	private EditText et_amount;
	private Button btn_outof;
	private TextView tv_record;
	private RelativeLayout rl_back;
	private TextView tv_customer;
	DecimalFormat df = new DecimalFormat("###0.00");
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ex_gold_out);
		initview();
		initData();
	}

	private void initview() {
		// TODO Auto-generated method stub
		et_amount=(EditText)findViewById(R.id.et_amount);
		btn_outof=(Button)findViewById(R.id.btn_ouof);
		tv_record=(TextView)findViewById(R.id.tv_record);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		tv_customer=(TextView)findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		tv_record.setOnClickListener(this);
		btn_outof.setOnClickListener(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("outmoney", jsonObject.toString());
				if(jsonObject.optInt("success")==1  ){
					
					JSONObject json1=jsonObject.optJSONObject("data");
					et_amount.setHint("最高可转出"+df.format(Double.parseDouble(json1.optString("availableAmount")))+"元");
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(ExperienceGoldOutActivity.this, Constant.URL+"/api/v5/experience/getAvailableMoney?", "");
	}
	private void outof(){
		AjaxParams params =new AjaxParams();
		params.put("outAmount", et_amount.getText().toString());
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if(jsonObject.optInt("success")==1){
					Log.e("体验金", jsonObject.toString());
					JSONObject json1=jsonObject.optJSONObject("data");
			 		Intent intent=new Intent(ExperienceGoldOutActivity.this,ExperienceGoldSuccess.class);
					intent.putExtra("amount",et_amount.getText().toString());
					intent.putExtra("comment",json1.optString("remark4"));
					startActivity(intent);
				}else{
					Toast.makeText(ExperienceGoldOutActivity.this, jsonObject.optString("comment"), 2000).show();
				}
				
			}
			
			@Override
			public void onError() {  
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(ExperienceGoldOutActivity.this, Constant.URL+"/api/v5/experience/confirmOutAmount?", params.toString());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()){
		case R.id.btn_ouof:
			if (et_amount.getText().toString().equals("")) {
				ToastUtil.showCenterToast(ExperienceGoldOutActivity.this,
						"请输入转出金额");
				return;
			}
			if (Double.parseDouble(et_amount.getText().toString())<=0) {
				ToastUtil.showCenterToast(ExperienceGoldOutActivity.this,
						"转出金额需大于0");
				return;
			}
			outof();
			break;    
		case R.id.tv_record:
			intent=new Intent(ExperienceGoldOutActivity.this,ExperienceGoldOutRecord.class);
			startActivity(intent);
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
		
	}

}
                                                 